Repository navigation
Model K (D3D12): coalesced 128-bit loads and exact I/O speedups (-0.173 ms at 1440p Q, byte-identical) - #297
phantoslayer21 wants to merge 7 commits into
Conversation
…73 ms at 1440p Q, byte-identical)
…r taps from the tiled input kernel (-40 us at 1440p Q, byte-identical) Three exact speedups on the native D3D12 device, none of which changes a value: - the output stage and the stage kernels keep each exact FMA chain behind a barrier so the driver does not regroup it; - k_stage2_d1 compiles at 191 VGPRs (8 workgroups per WGP); - k_color_fuse (default 1): the exact tiled input kernel also writes the output colour taps (kx/k_in_tiled_color[_uniform|_direct]), so the separate k_out_color launch is gone. k_color_fuse=0 brings it back. Windows AMD DX12 (RX 9070 XT, 1706x960 -> 2560x1440, HDR, auto-exposure, RGBA16F rows, 6 interleaved ABBA rounds, medians of 100 frames after warm-up): frame span 1540.0 -> 1498.7 us (-39.7 us, every round between -36.6 and -43.8 us); with k_color_fuse=0 1512.7 us (-25.6 us from the first two changes, -14.0 us from the colour taps). v0.6.5: 1721.7 us. Every output, intermediate and history buffer is byte-identical to the previous tip and to v0.6.5 (HDR value / texel / auto-exposure, LDR, rows and dense output, luma >= 1000 colours and out-of-range alpha, 1440p Quality and a small size, k_color_fuse 0 and 1, and the round-to-odd softmax form). k_smx_selfcheck.h: only kSourceDigest changes, the 133 reference hashes are the same.
…-masked 128-bit loads in the stage kernels (-7.9 us at 1440p Q, byte-identical) Two exact speedups on the native D3D12 device, neither changes a value: - kf_pm(a, b) = fma(a, b, -0.0 ^ opaque zero), bit for bit RN(a b) (the exact product plus -0.0 rounds once; a zero product keeps its sign, NaN / infinities / FTZ as the multiply): one v_fma_f32 with an SGPR addend instead of the multiply and the xor of kf_opq(a * b). The input stage (k_in*) and its shared helpers use it; the addend is unknown to the compiler, so the product is still never fused into a following addition. KF_PM = 0 brings back kf_opq(a * b); - KB_LD_A16 / KW_LD_A16 = 2: the 16-byte alignment of the stage kernels' 128-bit loads is proven on the scalar base only ((base & ~15) + off, the same addresses) and the driver emits the loads without the per-lane v_and_b32 -16 (RGA gfx1201). 1 = the whole address, 0 = none. k_smx_selfcheck.h: only kSourceDigest changes, the 133 reference hashes are the same. Windows AMD DX12 (RX 9070 XT, 1706x960 -> 2560x1440, HDR, auto-exposure, RGBA16F rows, 6 interleaved ABBA rounds, medians of 100 frames after warm-up): frame span 1491.7 -> 1482.2 us (-7.9 us paired, every round between -3.8 and -20.7 us); input stage -5.6 us, network -3.0 us, output +0.8 us, exposure unchanged. Every stage kernel gets shorter in the per-dispatch profile (-2.7 to -7.1 us each, k_stage2_e1 139.2 -> 135.0 us). v0.6.5: 1714.9 us. Every output, intermediate and history buffer is byte-identical to the previous tip (9 scenarios x 5 frames x 18 buffers = 810, HDR value / texel / auto-exposure, LDR, rows and dense output, luma >= 1000 colours and out-of-range alpha, 1440p Quality and a small size), and so are the 270 buffers of the round-to-odd softmax form (k_smx_test=mix_fail). The create-time softmax self-check passes (smx=mix). CPU: the Model K tests and the kernel-set test pass.
Setup and exactness
Both builds are byte-identical to the card's reference output: whole frames with every stage buffer, at six output sizes from 96x60 to 3840x2160. The new commits are exact on RADV. Where the new commits' time goesAt 1440p Quality, profiled per frame:
At 4K the pattern is the same: input plus colour -0.107 ms, ACO statistics and the likely costThese are ACO statistics for gfx1201, compiled without the card,
There are no spills anywhere. On RADV, the stage-kernel changes therefore add 6-16% instructions with no occupancy gain. My two local changesRebased onto current head, these are phase launches for stages e4/d4 by default on RDNA 4, plus the opt-in rolled e0 merge loop. Not tested: Windows, real model weights, RDNA 3, or QuestionWould you accept keeping the FMA-barrier form only where it helps the Windows driver, for example behind a define or only on kernels where the regrouping was seen, so RADV keeps the shorter code? |
…kernels (RADV keeps its 128-bit loads, byte-identical) KB_LD_A16 / KW_LD_A16 = 2 (8e1833f) masked the scalar base only. DXC writes base + (lane << 4) + c as (base & ~15) + (lane << 4 | c) there, and RADV's ACO does not read that as 16-byte aligned: the stage kernels' loads fell back to dword loads (+6 .. +16 % instructions in k_stage2_d1 / e1 / e0 / d0_hp and k_stage_e2 / d2, +0.024 ms at 1440p Q). The kernels' default is 1 again (the whole address masked, as in 19bfc54); 2 stays selectable with --define. The exact FMA barriers (wgk_fm), d1 at 191 VGPRs and the one-fma input stage are unchanged. k_smx_selfcheck.h: only kSourceDigest changes, the reference hashes are the same.
…r filter taps, colour taps skipped by halo-only waves (byte-identical by construction, RGA) k_out_pixel_c (all forms): the interior 5 x 5 p_L window as one 12-byte load per row (buffer_load_b96) instead of 25 16-bit loads, and, when a whole wave has row-major taps on the fast path, each line's three ctapT values from one 8-byte load (buffer_load_b64) instead of three 16-bit loads. The same bytes go into the same values; no arithmetic changes. RGA gfx1201: VMEM loads in the listing 110 -> 94, hot path 37 -> 9 for these taps, 80 VGPRs, no spills. k_in_tiled_color (all forms): a wave whose lanes own no render pixel in a pass of the tile loop skips ki_color_tap (the test is wave-uniform, so the conversions stay outside divergent branches); stored taps unchanged. GPU byte-identity and timing check still to run.
… 1440p Q vs 5c88a67, byte-identical) At 1440p Quality d1 has 1,025 windows: as 513 two-window workgroups (k_stage2_d1) they fill the 256 slots of four workgroups per CU for two rounds and one more (a single workgroup). k_stage_d1, the one-window form HIP's k_block.co already has (K_STAGES), runs the same per-window maths at 1,025 workgroups: 100-105 us instead of 134 us (WG_D3D12_PROFILE). Measured, the e0 / e1 one-window forms are slower (139 vs 120 us, 81 vs 78 us), so they stay two-window. Applies to every backend (stage_kernel_base); d0 is unchanged. - k_stage_d1__rodd: the round-to-odd form of the self-check / mix-probe fallback; isa_checks.json covers both forms (64 / 0 mix folds), kernel_sets.json updated. - softmax self-check: the d1 case is k_stage_d1 (smx_selfcheck_digest.py BASE); k_smx_selfcheck.h regenerated on HIP (gen_k_smx_selfcheck.py): the d1 hash is unchanged, only its name. - 0/6 diffs, 0 non-finite against 5c88a67: 1440p Q HDR pi 1.0 / 1.25 / 2.0, LDR pi 1.0, 645x363 -> 967x545 HDR. - 6-round ABBA against 5c88a67 (RX 9070 XT): pi 1.0 1.4623 -> 1.4430 ms, pi 1.25 1.4700 -> 1.4505, pi 2.0 1.4795 -> 1.4609.
…m-O tap forms, FP32 bias planes for d1 / e2 / d2 (-140 us at 1440p Q vs 5c88a67, byte-identical) k_out_pixel_c (every form, __a2 included), 537 -> 448 us at 1440p Q (WG_D3D12_PROFILE, RX 9070 XT): - HIP's non-plain best-E retry recomputed both pairs' E and ran in most waves (on the test frames 87 % of the waves have a sub-sample whose best E is below -12). It is now a per-sub-sample best-E tap tracked in the 12-tap loop: tap n replaces the best unless E[n] < best. Same result for every input, NaN included (the argument is in the source): without a NaN the sequential best is the last maximum, so best < -12 only when max E < -12; with a NaN the weight sum is NaN (the retry ran) and both forms end on tap 11; the two rules differ only at E[n] = -0, best = +0, a best that stays >= -0. -70 us on its own. - The taps' E are formed tap by tap for both sub-sample pairs (each pair's sums in tap order). A wave whose lanes all have the same O forms the pair-independent values once (ko_line_E modes 1 / 2: one FP16 value per pair where both halves held the same one, du / dv shared by the pairs). A test-only build that ran every lane through the uniform form of its own O (both forms on every pixel) gave 0/6 diffs on all five scenarios and 64/64 in the port check. - The 4.3 bilinear's four taps written out (the [unroll] loop with the q == 0 select stayed a rolled four-iteration loop of selects in the ISA, -10 us); kk = Yp / s3 in a branch (it was a select: the division ran on every pixel). - The 5 x 5 luma window and the 12 taps' T held as FP16 pairs: 84 -> 71 VGPRs (neutral at 1440p Q). kx/k_in_tiled_color* (k_frame.hlsli kio_encode, near branch): 1 / (Y + 1) as kio_rcp, the v_rcp_f32 + Newton step of ki_rcp / ko_rcp_refined for x in [2^-32, 2^32], wg_div elsewhere (= wg_div on every input): 217 -> 214 us. k_stage_d1 / k_stage_e2 / k_stage_d2 (and their __rodd forms): KB_BIAS32 + KB_BASE_FORM, the runtime uploads their FP32 bias planes (narrow_bias32_kernel): d1 99 -> 90 us, e2 / d2 within noise. Runtime: d3d12_kernels() names k_stage_d1, not k_stage2_d1 (prepared at create, checked against the payload), and the bias-plane ports required at create are the launched ones (test_hybrid_refuses_missing_compact_head_port failed at 3a35b73 on k_stage2_d1; passes, run alone). - k_smx_selfcheck.h regenerated on HIP (gen_k_smx_selfcheck.py): the 133 reference hashes are unchanged, only kSourceDigest. k_port_check.py: the three bias-plane kernels. test_k_alpha.py: the written-out bilinear. - 0/6 diffs, 0 non-finite against 5c88a67: 1440p Q HDR pi 1.0 / 1.25 / 2.0, LDR pi 1.0, 645x363 -> 967x545 HDR. - k_port_check.py (HIP against D3D12, special values): narrow 132/132, output 32/32, direct k_out / k_expo 28/28. The input family (kx/k_in_tiled) fails 32/32 the same way with 3a35b73's source: a harness issue, not traced here. - test_k_runtime_d3d12 (D3D12 against HIP, frame by frame): 37/38 in one run, the 38th (the refusal above, which reads WG_DX12_KERNELS at library load) passes run alone. Full build: ISA checks pass, kernel sets unchanged. - 6-round ABBA against 5c88a67 (RX 9070 XT): pi 1.0 1.4681 -> 1.3277 ms, pi 1.25 1.4735 -> 1.3348, pi 2.0 1.4791 -> 1.3400; against 3a35b73 at pi 1.0: 1.4594 -> 1.3393.
|
Updated branch to \c22762d\ (adds \3a35b73\ d1 one window per workgroup + \c22762d\ best-E retry folded into output tap loop, uniform-O tap forms, and FP32 bias planes for d1/e2/d2):
|
Summary
Bit-exact D3D12 kernel speedups for Model K on RDNA 4 (
gfx1201), centered on coalesced 128-bit loads and exact I/O optimizations ink_in.hlslandk_out.hlsl(plusk_frame.hlsli,k_in_common.hlsli,k_out.hlsli).Verification (RX 9070 XT
gfx1201, 1440p Quality1706x960 -> 2560x1440)1.6558 msmedian (1.6106 msmin)1.4826 msmedian (1.4618 msmin) — -0.1732 ms (-10.5%) bit-exact1.970 ms -> 1.617 ms(-0.353 ms)k_approx=a1:1.4147 msmedian (-0.2411 msvs v0.6.5 exact)smx_selfcheck_digestup to date (test_k_smx_selfcheck_digestpass)clean/k/tests: 155/155 unit tests pass (49 GPU/Linux-only skipped)tools/ci/check_line_endings.py: 0 problems